By Andrew Ma and Luke Miller
Julia is a high-level, high-performance dynamic programming language that looks like Ruby/Python syntax meets MatLab. It is meant to bridge the gap for mathematics and programming while also being very efficient at crunching numbers. Most of Julia's base library is written in Julia (woo metaprogramming).
In [1]:
# variable
x = 10
println(x)
# super hard math
y = x + 1
println(y)
# reassigning a variable
x = x + 1
println(x)
# unicode names
δ = 0.00001
println(δ)
안녕하세요 = "Hello"
println(안녕하세요)
Stylistic Conventions:
In [2]:
# Overflow example
x = typemax(Int64)
println(x)
println(x+1)
In [3]:
# Coefficients
x = 3
println(2x^2 - 3x + 1)
println(1.5x^2 - .5x + 1)
println(2^2x)
In [4]:
# Zero and One operators
println(zero(1.0))
println(one(0))
In [5]:
# Char
a = 'a'
println(a)
# String
string = "I'm a string"
println(string)
In [6]:
# Functions
function e(x,y)
x+y
end
function e2(x,y)
x+y, x-y
end
f(x,y) = x + y
g = f
∑(x,y) = x + y
println(e(1,2))
println(e2(1,2))
println(f(1,2))
println(g(1,2))
println(∑(1,2))
In [7]:
# Functions continued
println(+(1, 2, 3))
h = +
println(h(1,2,3))
println(map(x -> x^2 + 2x - 1, [1,3,-1]))
bar(a,b,x...) = (a,b,x)
println(bar(1,2,3,4,5,6))
function optionalArg(x,y,z=0)
x+y+z
end
println(optionalArg(1,2))
println(optionalArg(1,2,3))
In [8]:
# Scope
module A
a = 1 # a global in A's scope
end
module B
# b = a # would error as B's global scope is separate from A's
module C
c = 2
end
b = C.c # can access the namespace of a nested global scope
# through a qualified access
import A # makes module A available
d = A.a
# A.a = 2 # would error with: "ERROR: cannot assign variables in other modules"
end
Out[8]:
In [9]:
# Method
k(x::Number, y::Number) = 2x - y;
println(k(1,2))
In [10]:
# Things with types and arrays
num = 12
println(typeof(num))
println(convert(UInt8, num))
numArray = Any[1 2 3; 4 5 6]
println(typeof(numArray))
println(numArray)
convert(Array{Float64}, numArray)
# Define your own conversion
import Base.convert
convert(::Type{Bool}, x::Real) = x==0 ? false : x==1 ? true : throw(InexactError())
println(convert(Bool, 1))
println(convert(Bool, 0))
In [32]:
1+2
Out[32]:
In [11]:
(1+2)::AbstractFloat
In [12]:
(1+2)::Int
Out[12]:
Julia has a nice way to call a different method based on what types are passed into it: multiple dispatch Julia determines which function to dispatch the request to at run-time.
Example function headers:
function collide(me::Circle, other::Rectangle)
function collide(me::Polygon, other::Circle)
function collide(me::Polygon, other::Rectangle)
Then when you call
collide(me, other)
it dispatches it to the correct method
In [13]:
type Point
x::Float32
y::Float32
end
type Vector2D
x::Float32
y::Float32
end
type UnitVector2D
x::Float32
y::Float32
UnitVector2D(v::Vector2D) = (len = norm(v); new(v.x/len, v.y/len))
end
In [14]:
#Union Types:
VecOrUnit = Union{Vector2D, UnitVector2D}
dot(u::VecOrUnit, v::VecOrUnit) = u.x*v.x + u.y*v.y
Out[14]:
In [15]:
# Generate random 4x4 array
randomArray = rand(4,4)
Out[15]:
In [16]:
# Broadcasting allows for the easy element-by-element binary operation on arrays
broadcast(+, randomArray, randomArray)
Out[16]:
In [1]:
# Calling C code
t = ccall( (:clock, "libc"), Int32, ())
println(t)
path = ccall((:getenv, "libc"), Cstring, (Cstring,), "SHELL")
unsafe_string(path)
Out[1]:
Julia is designed for paralellization and does not impose any style of parallelization on its users.The following example demonstrates how to count the number of heads in a large number of coin tosses in parallel.
In [18]:
nheads = @parallel (+) for i=1:100000000
rand(Bool)
end
Out[18]:
In [19]:
@time nheads = @parallel (+) for i=1:100000000
rand(Bool)
end
Out[19]:
In [2]:
using DataFrames
In [21]:
# DataArray
dv = @data([NA, 3, 2, 5, 4])
println(mean(dv))
println(mean(dropna(dv)))
convert(Array, dropna(dv))
println(dv)
# converting na's
dv = @data([NA, 3, 2, 5, 4])
println(convert(Array, dv, 11))
In [22]:
df = DataFrame(A = 1:10, B = ["M", "F", "F", "M", "F", "M", "F", "F", "M", "M"])
Out[22]:
In [23]:
println(head(df))
println(tail(df))
println(df[1:3, :])
In [24]:
describe(df)
In [25]:
println(mean(df[:A]))
println(median(df[:A]))
In [26]:
df2 = DataFrame(A = 1:4, B = randn(4))
println(df2)
colwise(cumsum, df2)
Out[26]:
In [3]:
dataframe = readtable("train.csv")
head(dataframe)
Out[3]:
In [4]:
dataframe[:familysize] = dataframe[:SibSp] + dataframe[:Parch]
head(dataframe)
Out[4]:
In [5]:
dataframe[:Age] = convert(Array, dataframe[:Age], mean(dropna(dataframe[:Age])))
head(dataframe)
Out[5]:
In [6]:
head(dataframe[dataframe[:Sex] .== "male", :])
Out[6]:
In [7]:
using RDatasets
iris = dataset("datasets", "iris")
head(iris)
Out[7]:
In [ ]: